<template>
    <div class="common-info-container">
        <el-form label-width="80px" :model="commonInfo" class="common-info" :rules="rules" ref="commonInfoRef"
            :spellcheck="false" @submit.native.prevent :validate-on-rule-change="false">
            <el-form-item prop="glueSource" class="glueSource">
                <aceEditor v-model="commonInfo.glueSource" style="height: 100%;" :command="command"
                    :options="{ lang: type === 'shell' || type === 'commandline' ? 'sh' : type, fontSize: config.fontSize }" />
            </el-form-item>
        </el-form>
    </div>
</template>
<script setup>
import { config } from "@/utils/setting"
import router from "@/router"

let commonInfo = $ref({ glueSource: "" })
const commonInfoRef = $ref(null)

const props = defineProps({
    command: {
        type: Object,
        default: {}
    }
})

const route = router.currentRoute.value
let type = $ref(route.path.split('/').reverse()[0])

const validate = () => {
    return commonInfoRef.validate().catch((() => {
        return false
    }))
}

const checkScript = (rule, value, callback) => {
    if (!value) {
        return callback(new Error("脚本不能为空"))
    } else {
        callback()
    }
}

let rules = $ref({ glueSource: [{ validator: checkScript }] })

const save = () => {
    return { glueSource: commonInfo.glueSource }
}

const fill = (data) => {
    commonInfo.glueSource = data.glueSource
}

defineExpose({ validate, save, fill })
</script>
<style scoped>
:deep(.glueSource .el-form-item__content) {
    margin-left: 0 !important;
    margin-right: 0;
}

:deep(.glueSource .el-form-item__error) {
    top: 1px !important;
    right: unset;
    left: 50px;
    z-index: 999;
    pointer-events: none;
}
</style>

<style>
.source .el-message-box__header {
    border-bottom: 1px solid #ebeef5 !important;
}

.source .el-input__inner {
    height: 30px;
}

.source .el-message-box__input {
    padding-top: 5px;
    height: 32px;
}

.common-info-container,
.common-info,
.glueSource {
    height: 100%;
}
</style>
